<?php

define('DS', '\\');

error_reporting(E_ALL);

$pagesXML = new DOMDocument();
$pagesXML->load('data/pages.xml');

$xsl_dom = new DOMDocument();
$xsl_dom->load('get_array_pages.xsl');
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsl_dom);
$html = $xslt->transformToXml($pagesXML);

$pages = array();
eval($html);

$parameter_host = $_GET['host'];
$parameter_root_path = $_GET['path'];
$parameter_root_dir = $_GET['dir'];

function create_pages($pages)
{
    global $parameter_host;
    global $parameter_root_path;
    global $parameter_root_dir;


    $dataDOC = new DOMDocument();
    $dataDOC->load(dirname(realpath(__FILE__)).'/data/data.xml');

    $mainTemplateDOC = new DOMDocument();
    $mainTemplateDOC->load(dirname(realpath(__FILE__)).'/data/template.xsl');

    $mainTemplateXSL = new XSLTProcessor();
    $mainTemplateXSL->importStylesheet($mainTemplateDOC);

    foreach ($pages as $page_id => $page) {
        $templateDOC = new DOMDocument();
        $templateDOC->load(dirname(realpath(__FILE__))."/pages/{$page_id}/data.xsl");

        $templateXSL = new XSLTProcessor();
        $templateXSL->importStylesheet($templateDOC);
        $templateXSL->setParameter('', 'host', $parameter_host);
        $templateXSL->setParameter('', 'root-path', $parameter_root_path);
        $pageDataDOC = $templateXSL->transformToDoc($dataDOC);

        $mainTemplateXSL->setParameter('', 'host', $parameter_host);
        $mainTemplateXSL->setParameter('', 'root-path', $parameter_root_path);
        $html = $mainTemplateXSL->transformToXml($pageDataDOC);

        if (! file_exists(dirname(realpath(__FILE__)) . '/' . $parameter_root_dir . $page['path'])) {
            mkdir(dirname(realpath(__FILE__)) . '/' . $parameter_root_dir . $page['path']);
        }
        $html_file = fopen(dirname(realpath(__FILE__)) . '/' . $parameter_root_dir . $page['path'] . 'index.html', 'w');
        fwrite($html_file, $html);
        fclose($html_file);
    }
}

if (! file_exists(dirname(realpath(__FILE__)) . '/' . $parameter_root_dir .'/')) {
    mkdir(dirname(realpath(__FILE__)) . '/' . $parameter_root_dir .'/');
} else {
    remove(dirname(realpath(__FILE__)) . DS . $parameter_root_dir .DS);
    mkdir(dirname(realpath(__FILE__)) . DS . $parameter_root_dir .DS);
}

function remove($path) {
    $with_slash = ($path[strlen($path)-1] == '/' || $path[strlen($path)-1] == '\\');
    if (is_dir($path)) {
        $files = scandir($path);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                $fname = $path . ($with_slash ? $file : '/' . $file);
                remove($fname . (is_dir($fname) ? '/' : ''));
            }
        }
        rmdir($path);
    } else {
        unlink($path);
    }
}

function copy_dir($from, $to) {
    if (! is_dir($to)) {
        throw new Exception();
    }
    $with_slash_to = ($to[strlen($to)-1] == '/' || $to[strlen($to)-1] == '\\');
    $with_slash = ($from[strlen($from)-1] == '/' || $from[strlen($from)-1] == '\\');
    if (is_dir($from)) {
        $dir_name = $with_slash ? basename(($from)) : basename($from);
        mkdir($to . ($with_slash_to ? '' : DS) . $dir_name);
        $files = scandir($from);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                $_dir_name = $from . ($with_slash ? '' : DS);
                $_to_dir_name = $to . ($with_slash_to ? '' : DS);
                $_fname = $file . (is_dir($_dir_name . $file) ? DS : '');
                copy_dir($_dir_name . $_fname, $_to_dir_name . $dir_name);
            }
        }
    } else {
        copy($from, $to . ($with_slash_to ? '' : DS) . basename($from));
    }
}


$__files = scandir(dirname(realpath(__FILE__)) . DS .'root' . DS);
foreach ($__files as $__file) {
    if ($__file != '.' && $__file != '..') {
        copy_dir(dirname(realpath(__FILE__)) . DS .'root' . DS . $__file, dirname(realpath(__FILE__)) . DS .$parameter_root_dir .DS);
    }
}



create_pages($pages);

$host = $parameter_host;
$root_path = $parameter_root_path;
$root_url = 'http://' . $host . $root_path;

$css_text = file_get_contents(dirname(realpath(__FILE__)) . '/data/style.css');


$tmp_css_file = fopen(dirname(realpath(__FILE__)) . '/tmp_style.css', 'w');
fwrite($tmp_css_file, "<?php \$root = '{$root_path}'; ?>\n" . $css_text);
fclose($tmp_css_file);

$css = array();
$css_file_name = dirname(realpath(__FILE__)) . '/tmp_style.css';
exec('D:\WebServer\xampp\php\php.exe '.$css_file_name, $css);
$css = implode("\n", $css);
unlink($css_file_name);

$css_file = fopen(dirname(realpath(__FILE__)) . '/' . $parameter_root_dir . '/style.css', 'w');
fwrite($css_file, $css);
fclose($css_file);

?>